import ToolHttp from "@/tool/http/tool-axios.js";

let hkUrl = "";

export function getStreamUrl(
  cameraIndexCode,
  streamUrlProtocol = "PROTOCOL_RTSP"
) {
  return ToolHttp.get(
    `${hkUrl}/frame/hai-kang/get-stream-url?cameraIndexCode=${cameraIndexCode}&streamUrlProtocol=${streamUrlProtocol}&channelBusinessId=40289f5a7e3c994d017e3c998daf0000`
  );
}

export function getHlsUrl(cameraIndexCode) {
  return ToolHttp.get(
    `${hkUrl}/frame/hai-kang/get-stream-url?cameraIndexCode=${cameraIndexCode}&streamUrlProtocol=PROTOCOL_HLS&channelBusinessId=40289f5a7e3c994d017e3c998daf0000`
  );
}

export function customerSend(uri, jsonParams) {
  return ToolHttp.post(`${hkUrl}/frame/hai-kang/customer-do-post`, {
    channelBusinessId: "40289f5a7e3c994d017e3c998daf0000",
    uri,
    jsonParams: JSON.stringify(jsonParams),
  });
}

/**
 * 设置预制点信息
 * @param {Object} cameraIndexCode 摄像头编号
 * @param {Object} presetName 预置点名称
 * @param {Object} presetIndex 预置点编号
 */
export function presetPoint(cameraIndexCode, presetName, presetIndex) {
  return customerSend("/api/video/v1/presets/addition", {
    cameraIndexCode,
    presetName,
    presetIndex,
  });
}

/**
 * 查询预置点信息
 * @param {Object} cameraIndexCode
 */
export function queryPresetPoint(cameraIndexCode) {
  return customerSend("/api/video/v1/presets/searches", {
    cameraIndexCode,
  });
}

/**
 * 删除预置点信息
 * @param {Object} cameraIndexCode 摄像头编号
 * @param {Object} presetIndex 预置点编号
 */
export function delPresetPoint(cameraIndexCode, presetIndex) {
  return customerSend("/api/video/v1/presets/deletion", {
    cameraIndexCode,
    presetIndex,
  });
}

/**
 * 云台操作
 * @param {Object} cameraIndexCode 摄像头编号
 * @param {Object} action 0-开始 1-停止
 * @param {Object} command 具体操作
 * LEFT 左转
 * RIGHT右转
 * UP 上转
 * DOWN 下转
 * ZOOM_IN 焦距变大
 * ZOOM_OUT 焦距变小
 * LEFT_UP 左上
 * LEFT_DOWN 左下
 * RIGHT_UP 右上
 * RIGHT_DOWN 右下 FOCUS_NEAR 焦点前移
 * FOCUS_FAR 焦点后移 IRIS_ENLARGE 光圈扩大
 * IRIS_REDUCE 光圈缩小
 * WIPER_SWITCH 接通雨刷开关
 * START_RECORD_TRACK开始记录轨迹
 * STOP_RECORD_TRACK 停止记录轨迹
 * START_TRACK 开始轨迹
 * STOP_TRACK 停止轨迹
 * GOTO_PRESET到预置点(presetIndex 必穿)
 * @param {Object} speed 速度
 * @param {Object} presetIndex 预置点编号（需要运动到预置点才传）
 */
export function controlling(
  cameraIndexCode,
  action,
  command,
  presetIndex,
  speed = 50
) {
  return customerSend("/api/video/v1/ptzs/controlling", {
    cameraIndexCode,
    action,
    command,
    speed,
    presetIndex,
  });
}

/**
 * 手动抓图
 * @param {Object} cameraIndexCode 设备编号
 */
export function manualCapture(cameraIndexCode) {
  return customerSend("/api/video/v1/manualCapture", {
    cameraIndexCode,
  });
}

/**
 * 查询监控点列表v2
 * @param {Object} name 模糊查询字段
 * @param {Object} regionIndexCodes 区域编码列表（数组）
 * @param {Object} pageNo 页码
 * @param {Object} pageSize 每页数据量
 */
export function cameraSearch(
  cameraName,
  regionIndexCode,
  pageNo = 1,
  pageSize = 1000
) {
  return customerSend("/api/resource/v1/camera/advance/cameraList", {
    cameraName,
    regionIndexCode,
    pageNo,
    pageSize,
  });
}

/**
 * 区域列表树查询
 * @param {Object} params
 */

export async function areaSearch(params) {
  let res = await customerSend("/api/irds/v2/region/nodesByParams", {
    // camera	监控点
    // encodeDevice	编码设备
    // region	区域
    resourceType: "camera",
    isSubRegion: true,
    parentIndexCodes: ["root000000"],
    pageNo: 1,
    pageSize: 1000,
    ...params,
  });
  let regions = res;
  regions.forEach((item) => {
    item.children = regions.filter(
      (item_2) => item_2.parentIndexCode === item.indexCode
    );
  });

  return regions.filter((item_2) => item_2.parentIndexCode === "root000000");
}
/**
 * 获取跟区域信息
 */
export function areaRoot() {
  return customerSend("/api/resource/v1/regions/root", {});
}

/**
 * 保存设备配置(重复调用会更新设备配置)
 * @param {Object} code 设备编码
 * @param {Object} config 配置信息(传空返回现有配置)
 */
export function saveIotDeviceConfig(code, config) {
  return ToolHttp.post(`${hkUrl}/action/iot-device-config/${code}`, config);
}
/**
 * 查询AI实时流地址
 * @param {Object} data 参数
 */
export function getAiRealtimeUrl(data) {
  return ToolHttp.post("/action/ai-get-realtime-url/ai", data);
}

const hkApi = {
  controlling,
  delPresetPoint,
  queryPresetPoint,
  presetPoint,
  manualCapture,
  cameraSearch,
  areaSearch,
  areaRoot,
  saveIotDeviceConfig,
  getAiRealtimeUrl,
};

export default hkApi;
